<template>
  <div>
    <Myheader>
      <template #center>kiana rPwd</template>
      <template #os>换个密码玩玩</template>
    </Myheader>
    <WhiteSpace :space="100"/>
    <div class="container">
      <div class="left">
        <div class="bgi"></div>
      </div>
      <div class="right">
        <div class="form">
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="旧密码" prop="oldPass">
              <el-input type="password" v-model.number="ruleForm.oldPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPass">
              <el-input type="password" v-model.number="ruleForm.newPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model.number="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button :loading="buttonLoading" type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="bottom">
          ☆ ☆ ☆ ☆ ☆ Nekocharm! ☆ ☆ ☆ ☆ ☆
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { doRpwd } from '@/service/shop';
import { mapActions, mapState } from 'vuex';

export default {
  data() {
      var validateOldPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }  else {
          callback();
        }
      };
      var validateNewPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        }  else {
          callback();
        }
      };
      var validateCheckPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else {
          if (value !== this.ruleForm.newPass) {
            callback(new Error('新密码需要一致'));
          }
          callback();
        }
      };
      return {
        buttonLoading: false,
        ruleForm: {
          id: 0,
          oldPass: '',
          newPass: '',
          checkPass: '',
        },
        rules: {
          oldPass: [
            { validator: validateOldPass, trigger: 'blur' },
          ],
          newPass: [
            { validator: validateNewPass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validateCheckPass, trigger: 'blur' }
          ],
        }
      };
  },
  computed: {
    ...mapState('user', ['userInfo']),
  },
  methods: {
    ...mapActions('user', ['getUserInfoData', 'doLogoutData']),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.buttonLoading = true;
          const res = await doRpwd(this.ruleForm);
          console.log(res);
          this.buttonLoading = false;
          const { code, message } = res || {};
          if (code === 0) {
            return this.$message.error({
              message: '密码错误',
              duration: 1500,
              showClose: true,
            });
          }
          if (code === 1) {
            this.$message.success({
              message: message,
              duration: 1500,
              showClose: true,
            });
            // this.getUserInfoData();
            this.doLogoutData();
            console.log(this.$store.state.user);
          }
        } else {
          this.$message.error('请按要求填写表单');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    console.log(this.userInfo)
    this.ruleForm.id = this.userInfo.id;
  },
};
</script>

<style lang="scss" scoped>
.container {
  width: 70vw;
  height: 42vh;
  margin: 0 auto;
  display: flex;
  border: 1px solid pink;
  box-sizing: border-box;
  border-radius: 20px;
  box-shadow: 1px 2px 2px #ccc;
  overflow: hidden;
  .left {
    width: 20vw;
    // border-radius: 20px;
    background: url('@/assets/shenli.png');
    background-size: contain;

  }
  .right {
    width: 40vw;
    padding: 30px 20px 10px 0;
    box-sizing: border-box;
    .form {
      height: 35vh;
    }
    .bottom {
      height: 3vh;
      text-align: center;
      font-size: 14px;
      color: pink;
    }
  }
}
</style>
